package com.exposit.books.domain;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;

@Entity
@Table (name="buyers")
public class Buyer {
	
	private Long id;
	private String login;
	private String password;
	private String name;
	private String surname;
	private List<Book> likedBooks;
	private List<Comment> comments;
	private List<Order> orders;
		
	public Buyer(){}
	
	public Buyer(String login, String password, String name, String surname)
	{
		this.login=login;
		this.password=password;
		this.name=name;
		this.surname=surname;
	}	
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY )
	@Column(name = "buyer_id")
	@PrimaryKeyJoinColumn
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	@Column(unique=true, nullable=false)
	public String getLogin() {
		return login;
	}
	public void setLogin(String login) {
		this.login = login;
	}
	
	@Column(nullable=false)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(nullable=false)
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	@Column(nullable=false)
	public String getSurname() {
		return surname;
	}
	public void setSurname(String surname) {
		this.surname = surname;
	}
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "buyer_like_book", 
			joinColumns = { 
				@JoinColumn(name = "buyer_id", nullable = false) }, 
				inverseJoinColumns = { @JoinColumn(name = "book_id", nullable = false) })
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
	public List<Book> getLikedBooks() {
		return likedBooks;
	}
	public void setLikedBooks(List<Book> likedBooks) {
		this.likedBooks = likedBooks;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "buyer")
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
	public List<Comment> getComments() {
		return comments;
	}
	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}
	
	@OneToMany(fetch = FetchType.EAGER, mappedBy = "buyer")
	@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE})
	public List<Order> getOrders() {
		return orders;
	}
	public void setOrders(List<Order> orders) {
		this.orders = orders;
	}
}
